package dk.liljedahl.christian.apx.shipdaemon;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileMachine {
	public String GetScriptText(String fileToLoad){
		// Load the Script from a file
		// We want to return "" if no file was found / read exception
		String script = "";
		
		File file = new File(fileToLoad);
        StringBuffer contents = new StringBuffer();
        BufferedReader reader = null;
        file.exists();

        try {
            reader = new BufferedReader(new FileReader(file));
            String text = null;

            // repeat until all lines is read
            while ((text = reader.readLine()) != null) {
                contents.append(text)
                    .append(System.getProperty(
                        "line.separator"));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        if(contents.toString() != null){
        	script = contents.toString();
        }
		return script;
		
	}


	public void SetScriptText(String text, String fileToSave){
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(fileToSave));
			out.write(text);
			out.close();
		}
		catch (IOException e)
		{
			System.out.println("Exception ");		
		}
		
	}
}
